# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
import random

from scrapy import signals
import UAS

# useful for handling different item types with a single interface
from itemadapter import is_item, ItemAdapter

# 爬虫中间件
# class DoubanSpiderMiddleware:
#     """爬虫器中间件模板，默认是没有生效的，需要在配置文件settings中配置"""
#     # Not all methods need to be defined. If a method is not defined,
#     # scrapy acts as if the spider middleware does not modify the
#     # passed objects.
#
#     @classmethod
#     def from_crawler(cls, crawler):
#         # This method is used by Scrapy to create your spiders.
#         s = cls()
#         crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
#         return s
#
#     def process_spider_input(self, response, spider):
#         # Called for each response that goes through the spider
#         # middleware and into the spider.
#
#         # Should return None or raise an exception.
#         return None
#
#     def process_spider_output(self, response, result, spider):
#         # Called with the results returned from the Spider, after
#         # it has processed the response.
#
#         # Must return an iterable of Request, or item objects.
#         for i in result:
#             yield i
#
#     def process_spider_exception(self, response, exception, spider):
#         # Called when a spider or process_spider_input() method
#         # (from other spider middleware) raises an exception.
#
#         # Should return either None or an iterable of Request or item objects.
#         pass
#
#     def process_start_requests(self, start_requests, spider):
#         # Called with the start requests of the spider, and works
#         # similarly to the process_spider_output() method, except
#         # that it doesn’t have a response associated.
#
#         # Must return only requests (not items).
#         for r in start_requests:
#             yield r
#
#     def spider_opened(self, spider):
#         spider.logger.info('Spider opened: %s' % spider.name)


# 下载中间件
# class DoubanDownloaderMiddleware:
#     """下载器中间件模板"""
#     # Not all methods need to be defined. If a method is not defined,
#     # scrapy acts as if the downloader middleware does not modify the
#     # passed objects.
#
#     @classmethod
#     def from_crawler(cls, crawler):
#         # This method is used by Scrapy to create your spiders.
#         s = cls()
#         crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
#         return s
#
#     def process_request(self, request, spider):
#         # Called for each request that goes through the downloader
#         # middleware.
#
#         # Must either:
#         # - return None: continue processing this request
#         # - or return a Response object
#         # - or return a Request object
#         # - or raise IgnoreRequest: process_exception() methods of
#         #   installed downloader middleware will be called
#         return None
#
#     def process_response(self, request, response, spider):
#         # Called with the response returned from the downloader.
#
#         # Must either;
#         # - return a Response object
#         # - return a Request object
#         # - or raise IgnoreRequest
#         return response
#
#     def process_exception(self, request, exception, spider):
#         # Called when a download handler or a process_request()
#         # (from other downloader middleware) raises an exception.
#
#         # Must either:
#         # - return None: continue processing this exception
#         # - return a Response object: stops process_exception() chain
#         # - return a Request object: stops process_exception() chain
#         pass
#
#     def spider_opened(self, spider):
#         spider.logger.info('Spider opened: %s' % spider.name)





# 自定义下载器中间件
class AAAADownloaderMiddleware:
    def process_request (self, request, spider):
        """处理请求对象"""
        request.cookies = {'_vwo_uuid_v2': 'D3EBE0F7503441D3959402C3D71EEFF0B|4519ecc1b3f605c6b3697a14afd4a59b', '_ga': 'GA1.1.313176198.1627969259', '_ga_RXNMP372GL': 'GS1.1.1635228400.1.1.1635228631.0', 'douban-fav-remind': '1', 'gr_user_id': '3fdfb9be-b87a-4cc3-912f-8f8b07904bd0', 'bid': 'r1MwGRy5Gt0', 'll': '"118267"', '__utmv': '30149280.23554', '__gads': 'ID=ef8ce5743fefa86a-221beee43fd6009a:T=1662460703:RT=1662460703:S=ALNI_MbzxKdyJpTlJv1RdcDG0Vl1kAb72g', '__yadk_uid': 'C6KtjJ2f8urRFaZqGTH047G4srljKzDi', '__utmz': '223695111.1675861616.269.136.utmcsr=baidu|utmccn=(organic)|utmcmd=organic', 'ap_v': '0,6.0', '__utmc': '223695111', '__gpi': 'UID=0000054e17920d2a:T=1652440669:RT=1676284770:S=ALNI_MacE_FArVxgYZLUPPx0fB49PDm03Q', '_pk_ref.100001.4cf6': '%5B%22%22%2C%22%22%2C1676291660%2C%22https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DHSJxDDfxwtYFuP8pPMIMK5N0HuSgBgOd_8RQlcCWXH7fc9PY5o1IcN2rmlmt_8PqG-vDxb1kuZU1iN12bHbWPK%26wd%3D%26eqid%3De5b62929000454300000000663e37b1e%22%5D', '_pk_ses.100001.4cf6': '*', '_pk_id.100001.4cf6': '831bebd601806517.1627969259.280.1676291837.1676284769.', '__utma': '223695111.400287297.1627969259.1676284769.1676291837.274', '__utmb': '223695111.0.10.1676291837'}
        return None

# 构建一个添加用户代理的下载器中间件
class UADownloaderMiddleware:
    def process_request (self, request, spider):
        """处理请求对象"""
        user_agent_list = UAS.user_agent_list
        # 用户代理User-Agent的使用
        request.headers['User-Agent'] = random.choice(user_agent_list)
        # 代理IP的使用             协议    IP      端口
        request.meta['proxy'] = 'https://1.1.1.1:8888'

        # 跳转referer的设置
        referer = request.url

        if referer:
            request.headers['referer'] = referer
        return None